package com.koron.bean.query;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jzp
 * @since 2021/1/31 16:41
 */
@ToString
@Data
@ApiModel("分页对象")
public class PageQuery implements Serializable {
    /**
     * 当前页
     */
    @ApiModelProperty(value = "页码，默认第一页", example = "1")
    private int page = 1;

    /**
     * 每页显示条数，默认 15
     */
    @ApiModelProperty(value = "每页数据条数，默认15", example = "15")
    private int pageSize = 15;

    /**
     * 排序字段信息
     */
    @ApiModelProperty(value = "排序字段信息")
    private List<OrderItem> orders = new ArrayList<>();

    public PageQuery() {
    }

    public PageQuery(int page, int pageSize) {
        this.page = page;
        this.pageSize = pageSize;
    }

    public static PageQuery newPageQuery() {
        return new PageQuery();
    }

    /**
     * 不分页查询的PageQuery
     * exp:
     * <code>
     *     PageQuery pageQuery = PageQuery.newListQuery();
     *     PageHelper.startPage(pageQuery);
     * </code>
     *
     * 不分页查询但是要排序
     * <code>
     *     PageQuery pageQuery = PageQuery.newListQuery();
     *     // 。。。setOrderBy
     *     if (pageQuery.getPageNum() == 0) {
     *         PageHelper.orderBy(pageQuery.getOrderBy());
     *     } else {
     *         PageHelper.startPage(pageQuery);
     *     }
     * </code>
     */
    public static PageQuery newListQuery() {
        return new PageQuery(0, 0);
    }

    public int getPageNum() {
        return page;
    }

    public Boolean getPageSizeZero() {
        return true;
    }

    public List<OrderItem> getOrders() {
        return this.orders;
    }

    /**
     * exp:
     * code asc, create_time desc
     * 默认处理
     * @return order by参数
     */
    public String getOrderBy() {
        return getOrderBy(true);
    }

    /**
     * @modify zhouj
     * @param handle 是否处理 驼峰转大小写
     * @return order by参数
     *  true createTime --> create_time
     *  false createTime --> createTime
     */
    public String getOrderBy(boolean handle) {
        StringBuilder orderBy = new StringBuilder();
        for (int i = 0; i < orders.size(); i++) {
            orderBy.append(orders.get(i).getColumn(handle));
            orderBy.append(orders.get(i).isAsc() ? " asc" : " desc");
            if (i != orders.size() - 1) {
                orderBy.append(",");
            }
        }
        return orderBy.toString();
    }

    /**
     * 给指定字段加前缀
     * <pre>
     *  exp1:
     *   pageQuery.prefixOrders("user", null);
     *   pageQuery.getOrderBy() == "user.name asc, user.id asc"
     *
     *  exp2:
     *   pageQuery.prefixOrders("user", Arrays.asList("name"));
     *   pageQuery.getOrderBy() == "user.name asc, `id` asc"
     * </pre>
     * @param prefix 前缀
     * @param fields 需要加前缀的字段，为null表示所有字段
     */
    public void prefixOrders(String prefix, List<String> fields) {
        Assert.hasText(prefix, "前缀不能为空");
        if (CollectionUtils.isEmpty(fields)) {
            for (OrderItem order : orders) {
                order.setPrefix(prefix);
            }
        } else {
            for (OrderItem order : orders) {
                if (fields.contains(order.getColumn(false))) {
                    order.setPrefix(prefix);
                }
            }
        }
    }

    /**
     * 给指定字段之外的其他字段加前缀
     * @param prefix 前缀
     * @param fields 需要排除的字段
     */
    public void unPrefixOrders(String prefix, List<String> fields) {
        Assert.hasText(prefix, "前缀不能为空");
        if (!CollectionUtils.isEmpty(fields)) {
            for (OrderItem order : orders) {
                if (!fields.contains(order.getColumn(false))) {
                    order.setPrefix(prefix);
                }
            }
        }
    }

}

